Reporting with RMarkdown
1 RMarkdown
Is amazing.
1.1 What can RMarkdown be used for?
1.2 Key Resources
Key Reference: RMarkdown - The Definitive Guide
PDF Printing Setup: tinytex
2 How Rmarkdown Works
3 Header 1
3.1 Header 2
Header 3
4 Working with Text
Free-form text.
Make text bold.
Make text italics.
Make text bold + italics.
Talk about code - the tidyverse is awesome
Unordered List:
Item 1
Item 2
Ordered List:
First point
Second point
More points
5 Tabset
5.1 Tab 1
This is Tab 1
5.2 Tab 2
This is Tab 2
6 Images
7 Code
Read in data and print to HTML. Notice effect of df_print: paged option for HTML.
Try changing to
df_print: default, orkableortibble. PDF prints normally.Try changing
results = "hide".
# Bike data
bikes_tbl <- readRDS("01_data/bikes_tbl.rds")
bikeshops_tbl <- readRDS("01_data/bikeshops_tbl.rds")
orderlines_tbl <- readRDS("01_data/orderlines_tbl.rds")
bike_orderlines_tbl <- orderlines_tbl %>%
left_join(bikes_tbl, by = c("product_id" = "bike_id")) %>%
left_join(bikeshops_tbl, by = c("customer_id" = "bikeshop_id")) %>%
mutate(total_price = price_euro * quantity)
bike_orderlines_tblWe can do data manipulations too. Try changing the YAML code_folding option from none to hide to show.
sales_by_category_tbl <- bike_orderlines_tbl %>%
dplyr::select(category_2, category_1, total_price) %>%
group_by(category_2, category_1) %>%
summarise(total_revenue = sum(total_price)) %>%
ungroup() %>%
arrange(desc(total_revenue)) %>%
mutate(category_2 = as_factor(category_2) %>% fct_rev())#> `summarise()` has grouped output by 'category_2'. You can override using the
#> `.groups` argument.
8 Plots
Plotting works as expected. Try changin:
out.height,out.widthand KnittingPotential gotcha - Interactive plots (e.g.
plotly) will not display in PDF
Static Plots:
- Use
ggplot2.
Interactive plots:
- Use
ggplotly().
9 Tables
Static Tables:
knitrpackage -knitr::kable()- Simple to use, great with PDFgtpackage - Really good for static tables
table_formatted_tbl <- sales_by_category_tbl %>%
rename_all(.funs = ~ str_replace(., "_", " ") %>%
str_to_title())
table_formatted_tbl %>% knitr::kable()| Category 2 | Category 1 | Total Revenue |
|---|---|---|
| Race | Road | 11509156 |
| Trail | Mountain | 8644966 |
| Triathlon Bike | Road | 5831716 |
| Cross-Country | Mountain | 5421144 |
| Endurance | Road | 5013423 |
| E-Mountain | E-Bikes | 4962946 |
| All-Road | Gravel | 3697923 |
| Enduro | Mountain | 3156837 |
| City | Hybrid / City | 2115482 |
| Cyclocross | Road | 1940532 |
| E-Gravel | E-Bikes | 1936489 |
| Downhill | Mountain | 1803970 |
| E-City | E-Bikes | 1509096 |
| E-Trekking | E-Bikes | 1500894 |
| E-Fitness | E-Bikes | 1039996 |
| Touring | Hybrid / City | 877736 |
| Adventure | Gravel | 702007 |
| Fat Bikes | Mountain | 391654 |
| Dirt Jump | Mountain | 371922 |
| E-Road | E-Bikes | 2919 |
Top Bike Categories
Dynamic Tables:
- Can print tables without additional formatting in HTML with the
df_print: pagedoption in YAML - Potential Gotcha: Note that this will not print with format in PDF
10 Footnotes
This is some text with a Footnote1. This is a second Footnote2.

